#include "HullPolygonModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.hull_polygon_model.json";
}

HullPolygonModel::HullPolygonModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/hull_2d.html");
}

HullPolygonModel& HullPolygonModel::get()
{
    static HullPolygonModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void HullPolygonModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    filterFieldName_ = json["filterFieldName"].toString(filterFieldName_);
    filterLimitsMin_ = json["filterLimitsMin"].toDouble(filterLimitsMin_);
    filterLimitsMax_ = json["filterLimitsMax"].toDouble(filterLimitsMax_);

    segDistanceThreshold_ = json["segDistanceThreshold"].toDouble(segDistanceThreshold_);
    chullAlpha_ = json["chullAlpha"].toDouble(chullAlpha_);
}

QJsonObject HullPolygonModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("filterFieldName", QJsonValue(filterFieldName_));
    json.insert("filterLimitsMin", QJsonValue(filterLimitsMin_));
    json.insert("filterLimitsMax", QJsonValue(filterLimitsMax_));

    json.insert("segDistanceThreshold", QJsonValue(segDistanceThreshold_));
    json.insert("chullAlpha", QJsonValue(chullAlpha_));

    return json;
}
